package com.googlecode.economini.converter;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.List;

import org.ccil.cowan.tagsoup.Parser;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class Converter<T extends Enum<?>> {

  private final TokenizingHandler<T> tokenizingHandler;

  public Converter(TokenizerNode<T> rootNode) {
    tokenizingHandler = new TokenizingHandler<T>(rootNode);
  }

  public List<Token<T>> parse(Reader reader) throws IOException, SAXException {
    return parse(new InputSource(reader));
  }

  public List<Token<T>> parse(InputStream in) throws IOException, SAXException {
    return parse(new InputSource(in));
  }

  public List<Token<T>> parse(InputSource inputSource) throws IOException,
      SAXException {
    Parser parser = new Parser();
    parser.setContentHandler(tokenizingHandler);
    parser.parse(inputSource);
    return tokenizingHandler.getTokens();
  }

  // public static void main(String[] args) throws Exception {
  // FileReader reader = new FileReader(args[0]);
  //
  // TokenizerNode title = new TokenizerNode(
  // "h3",
  // new AttributeMatcher.Builder()
  // .matchesAll()
  // .withAttribute("class", "headline")
  // .build(),
  // TokenType.TITLE,
  // ImmutableList.<TokenizerNode>of());
  //
  // TokenizerNode sectionTitle = new TokenizerNode(
  // "strong",
  // AttributeMatcher.none(),
  // TokenType.SECTION_HEADER,
  // ImmutableList.<TokenizerNode>of());
  //
  // TokenizerNode section = new TokenizerNode(
  // "p",
  // AttributeMatcher.none(),
  // TokenType.SECTION,
  // ImmutableList.<TokenizerNode>of(sectionTitle));
  //
  // TokenizerNode articleContent = new TokenizerNode(
  // "div",
  // new AttributeMatcher.Builder()
  // .withAttribute("class", "ec-article-content clear")
  // .build(),
  // TokenType.NONE,
  // ImmutableList.<TokenizerNode>of(section));
  //
  // TokenizerNode articleBody = new TokenizerNode(
  // "div",
  // new AttributeMatcher.Builder()
  // .withAttribute("id", "ec-article-body")
  // .withAttribute("class", "clearfix")
  // .build(),
  // TokenType.NONE,
  // ImmutableList.<TokenizerNode>of(articleContent, title));
  //
  // TokenizerNode rootN = buildNodeForPath(
  // articleBody,
  // ImmutableList.of("root", "html", "body", "div", "div", "div", "div"));
  //
  // new Converter(reader, rootN).parse();
  // }
}
